home *** CD-ROM | disk | FTP | other *** search
- /*
- ChosenPrinter XFCN v1.1
-
- ©1991 Apple Computer, Inc.; by Mike Byrne
-
- Returns what type of printer is currently selected, and, if it can find it, what the name
- of said printer is in item 2 of the return; i.e. it might return "LaserWriter,Godzilla" if
- Godzilla is the currently selected LaserWriter.
-
- Form:
- ChosenPrinter()
-
- # the MPW 3.2 build commands:
- C -b ChosenPrinter.c -mbg off
- Link -w -t STAK -c WILD -rt XFCN=615 ∂
- -m ENTRYPOINT ∂
- -sg ChosenPrinter ∂
- ChosenPrinter.c.o ∂
- "{Libraries}HyperXLib.o" ∂
- "{Libraries}Runtime.o" ∂
- "{Libraries}Interface.o" ∂
- "{CLibraries}StdCLib.o" ∂
- -o "teststack"
- */
-
- #include <Types.h>
- #include <Resources.h>
- #include <string.h>
- #include <Memory.h>
- #include <Packages.h>
- #include <Errors.h>
- #include "HyperXCmd.h"
-
- #define NULL '\0'
- #define NIL 0L
- #define TRUE 1
- #define FALSE 0
-
- #define kNumParams 0
-
-
- /* prototypes */
- void ErrorBack(XCmdPtr paramPtr, char *message);
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
- void UnlockParams ( XCmdPtr paramPtr, short paramCount );
- short SystemFolderRefNum(void);
- char* GetFirst255(Ptr resPtr);
-
-
- pascal void EntryPoint(XCmdPtr paramPtr)
- {
- /* variable declarations */
- Boolean* orgResLoadPtr;
- Boolean orgResLoad;
- short orgResFile;
- char printerName[260];
- char dummyString[260];
- OSErr theErr;
- Handle resHandle;
- short printerResFile;
- short sysFolder;
-
-
- /* move high and lock the parameters. */
- MoveLockParams(paramPtr, paramPtr->paramCount);
-
- /* check for copyright or syntax help request */
- if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
- ErrorBack(paramPtr, "v1.1, ©1991 Apple Computer, Inc.; by Mike Byrne");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
- ErrorBack(paramPtr, "ChosenPrinter syntax is 'ChosenPrinter()'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* not a copyright or help request. */
- /* check for correct number of parameters */
- if (paramPtr->paramCount != kNumParams) {
- ErrorBack(paramPtr, "Error: ChosenPrinter syntax is 'ChosenPrinter()'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* First, save the old state. */
- orgResFile = CurResFile();
- (long) orgResLoadPtr = 0xA5E;
- if (*orgResLoadPtr) {
- orgResLoad = TRUE;
- } else {
- orgResLoad = FALSE;
- }
-
- /* set the system file for the string and get it. */
- UseResFile(0);
- SetResLoad(TRUE);
- resHandle = Get1Resource('STR ',-8192);
- theErr = ResError();
- if ((theErr != noErr) || (resHandle == NIL)) {
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- if ((theErr == resNotFound) || (resHandle == NIL)) {
- ErrorBack(paramPtr, "No printer is selected.");
- } else {
- ErrorBack(paramPtr, "Error: Could not get the 'STR ' resource.");
- }
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* now we have the printer name */
- strcpy(printerName, GetFirst255(*resHandle));
-
- /* try to get the ID number for the current system folder. */
- if ( !(sysFolder = SystemFolderRefNum())) {
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- ErrorBack(paramPtr, printerName);
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* open the printer file. If there are any errors, just return the printer
- type and don't worry about it. */
- c2pstr(printerName);
- printerResFile = OpenRFPerm(printerName, SystemFolderRefNum(), fsRdPerm);
- p2cstr(printerName);
- theErr = ResError();
- if (theErr != noErr) {
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- ErrorBack(paramPtr, printerName);
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* get the PAPA resource. Again, if none, leave with the printer name... */
- UseResFile(printerResFile);
- resHandle = Get1Resource('PAPA',-8192);
- theErr = ResError();
- if (theErr != noErr) {
- CloseResFile(printerResFile);
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- ErrorBack(paramPtr, printerName);
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* only cat to the printer type if we have something. */
- strcpy( dummyString, GetFirst255(*resHandle) );
- if (strlen(dummyString) > 0) {
- strcat( printerName, ",");
- strcat( printerName, dummyString );
- }
-
- /* clean up and go home. */
- CloseResFile(printerResFile);
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- ErrorBack(paramPtr,printerName);
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
-
- /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- GetFirst255 takes a handle pointing to some record beginning with a Str255.
- It reads the length byte and BlockMoves to the c string. */
-
- char* GetFirst255(Ptr resPtr)
- {
- Byte* lengthPtr;
- short length;
- char retString[260];
-
- (Ptr) lengthPtr = resPtr;
- length = (short) *lengthPtr;
- lengthPtr++;
- BlockMove( (Ptr) lengthPtr, (Ptr) retString, (Size) length);
- retString[length] = NULL;
- return(retString);
- }
-
-
- /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- SystemFolderRefNum gives the directory id of the system folder. */
-
- short SystemFolderRefNum(void)
- {
- SysEnvRec theWorld;
- short volID;
- char pathName[256];
- char cDummy[128];
- Str255 pasDummy;
-
- /* set strings to zero */
- pathName[0] = NULL;
- cDummy[0] = NULL;
- pasDummy[0] = NULL;
-
- /* get the ref number for the system folder */
- if (SysEnvirons(1, &theWorld)) {
- return(0);
- } else {
- volID = theWorld.sysVRefNum;
- }
- return(volID);
-
- }
-
-
-
-
-
- /* allocate and load up paramPtr->returnValue with a string
- -------------------------------------------------------- */
- void ErrorBack(XCmdPtr paramPtr, char *message)
- {
- Handle mesHnd;
-
- /*
- Allocate space for an error message.
- Copy the string into it.
- Return the handle to HyperCard.
- */
- mesHnd = NewHandle((long)(strlen(message)+1));
- if (mesHnd == nil) return;
- strcpy((char *)*mesHnd,message);
- paramPtr->returnValue = mesHnd;
- }
-
-
-
- /* move high and lock down all parameters
- ----------------------------------------------------------------------- */
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
- {
- short i;
-
- for(i=0; i <= paramCount-1; i++)
- {
- MoveHHi(paramPtr->params[i]);
- HLock(paramPtr->params[i]);
- }
- }
-
-
-
-
- /* unlock all parameter handles in the XCmdBlock
- --------------------------------------------- */
- void UnlockParams ( XCmdPtr paramPtr, short paramCount )
- { short i;
-
- for(i=0; i <= paramCount-1; i++)
- { HUnlock(paramPtr->params[i]);}
- }
-